<?php

	class manga_model extends st_model {
		protected $_instance  = array(
										  'table'      => 'catalog_manga', 
										  'primaryKey' => 'id',
									  );
									  
		public function getMangaUrl() 
		{
			return site_url('admin/catalog/manga/edit/id/' . $this->getId(), true);
		}
		
		public function getIndexUrl($param = '') 
		{
			return site_url() . '/admin/catalog/manga/index/' . $param;
		}
		
		public function getCategoryIds()
		{
			$_categoryIds = array();
			if (!$this->isLoaded()) {
				return $_categoryIds;
			} 
			$query = 'select category_id from catalog_category_manga where manga_id = '.$this->getId();
			$_res = $this->db->query($query);
			if ($_res->num_rows() > 0) {
				foreach ($_res->result() as $row) {
					$_categoryIds[] = $row->category_id;
				}
			}
			$_res->free_result();
			return $_categoryIds;
		}
		
		public function getCategoryCollection()
		{
			if (!$this->isLoaded()) {
				return null;
			} 
			$collection = array();
			$categoryIds = $this->getCategoryIds();
			if (count($categoryIds) > 0) {
				foreach ($categoryIds as $id) {
					$model = new category_model();
					$collection[$id] = $model;
				}
			}
			return $collection;
		}
		
		public function getChapterCollection()
		{
			if (!$this->isLoaded()) {
				return array();
			}
			$this->load->model('manga_chapter_model');
			$collection = $this->manga_chapter_model
							   ->addAttributeToFilter('manga_id', $this->getId())
							   ->getCollection();
			if (!$collection)
			{
				return array();
			}
			return $collection;
		}
		
		public function getThumbUrl()
		{
			if (!$this->isLoaded()) {
				return null;
			}
			$url = base_url() . substr($this->getThumbImage(), 1, strlen($this->getThumbImage()));
			return $url;
		}
	}	
?>